package com.itheima.tlias84.aspect;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Objects;

/**
 * 统计接口耗时的切面逻辑
 *
 * @Component 让Spring管理，自动注入
 */
@Slf4j
@Aspect
@Component
public class CountTimeAspect {
    @Resource
    HttpServletRequest request;



//点    @Around("execution(* com.itheima.tlias84.service.impl.DeptServiceImpl.selectAll(..))")
//线    @Around("execution(* com.itheima.tlias84.service.impl.DeptServiceImpl.*(..))")
    @Around("execution(* com.itheima.tlias84.service.impl.*.*(..))")
    public Object count(ProceedingJoinPoint point) {
        //1、获取操作人
//        String token = request.getHeader("token");
//        log.info("获取token：{}",token);
//        if (Objects.nonNull(token)){
//            JSONObject object = JwtUtil.verifyToken(token);
//            String username = object.getString("username");
//            log.info("获取操作人：{}",username);
//        }
        //2、获取参数
        Object[] args = point.getArgs();
        log.info("获取参数：{}",args);
        //3、获取类名-父类的-->看你还要处理这个数据
        Object target = point.getTarget();
        log.info("获取类名：{}",target);
        long start = System.currentTimeMillis();
        //执行父类方法-->用注解来指定方法
        try {
            Object proceed = point.proceed();
            //4、获取返回值
            log.info("获取返回值：{}",proceed);
            long end = System.currentTimeMillis() - start;
            //如果我想知道是哪个接口耗时，可以打印point.getSignature()
            log.info("{}--执行耗时：{}",point.getSignature().getName(), end);
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
